package JavaBase.ArrayTest;

/**
 * @Author：coinyang
 * @Project：JavaTest
 * @name：MonkeyKing
 * @Date：2023/8/3 17:40
 */
public class MonkeyKing {
        public static void main(String[] args) {
            int monkeyKing = findMonkeyKing();
            System.out.println("最后成为大王的猴子编号是：" + monkeyKing);
        }

        private static int findMonkeyKing() {
            int[] monkeys = new int[15];  // 用0表示猴子存活状态

            int count = 0;
            int monkeyCount = monkeys.length;
            while (monkeyCount > 1) {
                for (int i = 0; i < monkeys.length; i++) {
                    if (monkeys[i] == 0) {  // 如果猴子存活
                        count++;
                        if (count == 7) {
                            monkeys[i] = 1;  // 淘汰猴子
                            count = 0;
                            monkeyCount--;
                        }
                    }
                }
            }

            for (int i = 0; i < monkeys.length; i++) {
                if (monkeys[i] == 0) {
                    return i + 1;  // 返回最后剩下的猴子编号
                }
            }

            return -1;  // 出错情况，未找到结果时返回-1
        }

}
